import util from '@/js/util/util.js';

export function getBoxStyle(element) {
    let style = {};
    if (element) {
        style.top = element.style.top.pixel + 'px';
        style.left = element.style.left.pixel + 'px';
        style.width = element.style.width.pixel + 'px';
        style.height = element.style.height.pixel + 'px';
        if (util.isNotBlank(element.style.rotate)) {
            style.transform = 'rotate(' + element.style.rotate + 'deg)';
        }
        if (util.isNotBlank(element.style['z-index'])) {
            style['z-index'] = element.style['z-index'];
        }
    }
    return style;
};

export function getElementStyle(element) {
    let style = {};
    if (element && element.style) {
        style.width = element.style.width.pixel + 'px';
        style.height = element.style.height.pixel + 'px';
        if (util.isNotBlank(element.style.backgroundImage) && element.style.backgroundImage.color.length > 0) {
            let list = [];
            for (let i = 0; i < element.style.backgroundImage.color.length; i++) {
                if (element.style.backgroundImage.color[i]) {
                    list.push(element.style.backgroundImage.color[i]);
                }
            }
            if (list.length == 1) {
                style.backgroundColor = list[0];
            } else if (list.length >= 2) {
                let angle = 90;
                if (element.style.backgroundImage.angle || element.style.backgroundImage.angle == 0) {
                    angle = element.style.backgroundImage.angle;
                }
                let backgroundImage = 'linear-gradient(' + angle + 'deg';
                let len = list.length - 1;
                for (let i = 0; i < list.length; i++) {
                    let percent = ((i) / len * 100).toFixed(2);
                    backgroundImage += ',' + list[i] + ' ' + percent + '%';
                }
                backgroundImage += ')';
                style.backgroundImage = backgroundImage;
            }
        }
        if (util.isNotBlank(element.style.opacity)) {
            style.opacity = element.style.opacity / 100;
        }
        if (util.isNotBlank(element.style.color)) {
            style.color = element.style.color;
        }
        if (util.isNotBlank(element.style.fontSize)) {
            // style.fontSize = element.style.fontSize + element.style.fontSize.unit;
            style.fontSize = element.style.fontSize + 'px';
        }
        if (util.isNotBlank(element.style.fontWeight)) {
            style.fontWeight = element.style.fontWeight;
        }
        if (util.isNotBlank(element.style.direction)) {
            style.direction = element.style.direction;
        }
        if (util.isNotBlank(element.style.textAlign)) {
            style.textAlign = element.style.textAlign;
        }
        if (util.isNotBlank(element.style.verticalAlign)) {
            style.verticalAlign = element.style.verticalAlign;
        }
        if (util.isNotBlank(element.style.borderRadius)) {
            style.borderRadius = element.style.borderRadius.value + element.style.borderRadius.unit;
        }
        if (util.isNotBlank(element.style.borderWidth)) {
            style.borderWidth = element.style.borderWidth + 'px';
        }
        if (util.isNotBlank(element.style.borderStyle)) {
            style.borderStyle = element.style.borderStyle;
        }
        if (util.isNotBlank(element.style.borderColor)) {
            style.borderColor = element.style.borderColor;
        }
        if (util.isNotBlank(element.style.boxShadow)) {
            style.boxShadow = `${element.style.boxShadow.level}px ${element.style.boxShadow.vertical}px ${element.style.boxShadow.blur}px ${element.style.boxShadow.spread}px ${element.style.boxShadow.color}`;
        }
        if (util.isNotBlank(element.style.lineHeight)) {
            style.lineHeight = element.style.lineHeight + 'px';
        }
        if (util.isNotBlank(element.style.letterSpacing)) {
            style.letterSpacing = element.style.letterSpacing + 'px';
        }
        if (util.isNotBlank(element.style.fontFamily)) {
            style.fontFamily = element.style.fontFamily;
        }
    }
    return style;
};

let style = {
    getBoxStyle: function(element) {
        let style = {};
        if (element) {
            style.top = element.style.top.pixel + 'px';
            style.left = element.style.left.pixel + 'px';
            style.width = element.style.width.pixel + 'px';
            style.height = element.style.height.pixel + 'px';
            if (util.isNotBlank(element.style.rotate)) {
                style.transform = 'rotate(' + element.style.rotate + 'deg)';
            }
            if (util.isNotBlank(element.style['z-index'])) {
                style['z-index'] = element.style['z-index'];
            }
        }
        return style;
    },
    getElementStyle: function(element) {
        let style = {};
        if (element && element.style) {
            style.width = element.style.width.pixel + 'px';
            style.height = element.style.height.pixel + 'px';
            if (util.isNotBlank(element.style.backgroundImage) && element.style.backgroundImage.color.length > 0) {
                let list = [];
                for (let i = 0; i < element.style.backgroundImage.color.length; i++) {
                    if (element.style.backgroundImage.color[i]) {
                        list.push(element.style.backgroundImage.color[i]);
                    }
                }
                if (list.length == 1) {
                    style.backgroundColor = list[0];
                } else if (list.length >= 2) {
                    let angle = 90;
                    if (element.style.backgroundImage.angle || element.style.backgroundImage.angle == 0) {
                        angle = element.style.backgroundImage.angle;
                    }
                    let backgroundImage = 'linear-gradient(' + angle + 'deg';
                    let len = list.length - 1;
                    for (let i = 0; i < list.length; i++) {
                        let percent = ((i) / len * 100).toFixed(2);
                        backgroundImage += ',' + list[i] + ' ' + percent + '%';
                    }
                    backgroundImage += ')';
                    style.backgroundImage = backgroundImage;
                }
            }
            if (util.isNotBlank(element.style.opacity)) {
                style.opacity = element.style.opacity / 100;
            }
            if (util.isNotBlank(element.style.color)) {
                style.color = element.style.color;
            }
            if (util.isNotBlank(element.style.fontSize)) {
                style.fontSize = element.style.fontSize + 'px';
            }
            if (util.isNotBlank(element.style.fontWeight)) {
                style.fontWeight = element.style.fontWeight;
            }
            if (util.isNotBlank(element.style.direction)) {
                style.direction = element.style.direction;
            }
            if (util.isNotBlank(element.style.textAlign)) {
                style.textAlign = element.style.textAlign;
            }
            if (util.isNotBlank(element.style.verticalAlign)) {
                style.verticalAlign = element.style.verticalAlign;
            }
            if (util.isNotBlank(element.style.borderRadius)) {
                style.borderRadius = element.style.borderRadius.value + element.style.borderRadius.unit;
            }
            if (util.isNotBlank(element.style.borderWidth)) {
                style.borderWidth = element.style.borderWidth + 'px';
            }
            if (util.isNotBlank(element.style.borderStyle)) {
                style.borderStyle = element.style.borderStyle;
            }
            if (util.isNotBlank(element.style.borderColor)) {
                style.borderColor = element.style.borderColor;
            }
            if (util.isNotBlank(element.style.boxShadow)) {
                style.boxShadow = `${element.style.boxShadow.level}px ${element.style.boxShadow.vertical}px ${element.style.boxShadow.blur}px ${element.style.boxShadow.spread}px ${element.style.boxShadow.color}`;
            }
            if (util.isNotBlank(element.style.textShadow)) {
                if (element.style.textShadow.visible) {
                    style.textShadow = `${element.style.textShadow.level}px ${element.style.textShadow.vertical}px ${element.style.textShadow.blur}px ${element.style.textShadow.color}`;
                } else {
                    style.textShadow = null
                }
            }
            if (util.isNotBlank(element.style.backdropFilter)) {
                style.backdropFilter = `blur(${element.style.backdropFilter}px)`;
            }
            if (util.isNotBlank(element.style.filter)) {
                style.filter = `blur(${element.style.filter}px)`;
            }
            if (util.isNotBlank(element.style.lineHeight)) {
                style.lineHeight = element.style.lineHeight + 'px';
            }
            if (util.isNotBlank(element.style.letterSpacing)) {
                style.letterSpacing = element.style.letterSpacing;
            }
            if (util.isNotBlank(element.style.fontFamily)) {
                style.fontFamily = element.style.fontFamily;
            }
        }
        return style;
    }
};

export default style;